<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'粉丝会员列表'),
	array('op'=>'zhun','title'=>'无粉丝会员列表'),
	array('op'=>'xnlist','title'=>'虚拟会员列表'),
	array('op'=>'ex_demo','title'=>'导入虚拟用户'),
	array('op'=>'chack_del','title'=>'清空虚拟用户'),
	array('op'=>'heti','title'=>'合并会员'),
	array('op'=>'daoru','title'=>'CSV导入会员'),
);
$return['groups']=pdo_getall('core_member_groups',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
if($_RQ['op']=='list'){
	//会员列表
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'onemid'=>0,'is_virtual'=>0,'nickname <>'=>'');
	if($_RQ['id']){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	if(isset($_RQ['groupid'])){
		$params['groupid']=intval($_RQ['groupid']);
		$return['params']['groupid']=$_RQ['groupid'];
	}
	if(!empty($_RQ['nickname'])){
		$params['nickname like']='%'.$_RQ['nickname'].'%';
		$return['params']['nickname']=$_RQ['nickname'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if(!empty($_RQ['username'])){
		$params['username like']='%'.$_RQ['username'].'%';
		$return['params']['username']=$_RQ['username'];
	}
	if(!empty($_RQ['fromuser'])){
		$params['fromuser']=intval($_RQ['fromuser']);
		$return['params']['fromuser']=$_RQ['fromuser'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:10;
	$fields=array('id','avatar','nickname','username','mobile','credit1','credit2','credit3','credit4','credit5','credit6','createtime','fromuser','from_os','nocheck');
	$list=pdo_getslice('core_members',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['creditlog']=array('action'=>'member.creditlog','title'=>"会员{$item['nickname']}的积分日志",'query'=>'mid='.$item['id']);
			$fans=pdo_get('core_fans',array('mid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),array('id','mid','wxgid','aligid','ttgid','baidugid'));
			if(!$fans['wxgid'] && $fans['aligid']){
				pdo_update('core_fans',array('wxgid'=>$fans['aligid']),array('id'=>$fans['id'],'uniacid'=>$_SESSION['uniacid']));
			}
			if(!$fans['wxgid'] && $fans['ttgid']){
				pdo_update('core_fans',array('wxgid'=>$fans['ttgid']),array('id'=>$fans['id'],'uniacid'=>$_SESSION['uniacid']));
			}
			if(!$fans['wxgid'] && $fans['baidugid']){
				pdo_update('core_fans',array('wxgid'=>$fans['baidugid']),array('id'=>$fans['id'],'uniacid'=>$_SESSION['uniacid']));
			}
			$item['wxapptitle']=pdo_getcolumn('core_account_wxapp',array('uniacid'=>$_SESSION['uniacid'],'id'=>$fans['wxgid']),'title');
			if($fans['wxgid'] && !$item['wxapptitle']){
				$item['wxapptitle']='小程序已删除';
			}
			$item['from_os']=get_ostitle()[$item['from_os']];
			$item['creditdata']=model('core/member')->creditdata($item);
			$item['member']=cfc('core')->member_show($item['id']);
			$item['shmember']=cfc('core')->member_show($item['fromuser']);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'export'=>'export',
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：
		</br>1、关于<red>匿名用户/空白会员</red>的解释：正常认证服务号，没有关闭获取粉丝详细的情况下，是不会存在匿名用户的；可能小程序出现匿名用户的情况居多：原因是：小程序官方不允许首次访问弹窗获取粉丝详细。但是为了上下级的准确性，在访问小程序首页的时候就产生了下级关系！这时候就会出现匿名用户的情况。但是只要这个匿名用户访问了购物车，商品，会员中心等任何页面。就会弹窗获取粉丝信息。这时候匿名用户就会变成带昵称头像的会员了！
		</br><red>【注意】：虽然头像，昵称一样，但是小程序会员跟H5的会员数据是不同步的（除非开启粉丝互通--移步：站点设置-全网粉丝互通）！开启互通后，粉丝互通只对新用户有效，对于互通前已产生的会员无效（考虑已产生的会员本身已经存在很多不确定的数据，故不考虑合并！）</red>
		</br><red>【温馨提示】：
		</br>1、如果小程序绑定了微信开放平台，小程序的上传版本，就要到微信开放平台来审核，发布版本！
		</br>2、小程序必须绑定微信开放平台才能互通粉丝，而不是微信公众号那边的简单关联！</red>
		</br>3、【<red>多个昵称,头像一样的会员</red>】？每个公众号，每个小程序（微信/支付宝/头条），都会有自己的唯一粉丝会员，固然就会出现多个雷同会员。
		</br>4、【<red>多个雷同会员如何统一</red>】？1、只需绑定同样的手机号，会员就会统一；2、利用微信开放平台统一粉丝会员（这个方法操作较复杂）；
		</br>5、【<red>手机统一会员的原理</red>】？绑定统一手机号后，同手机的会员账户登录时，会统一自动登录一个较老的会员账户，所有的会员ID信息都是较老这个！
		','del_message'=>'确认删除该会员吗？','total'=>$total,'psize'=>$psize,'del_op'=>'del','html_to'=>'list_html','list'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'shmember','title'=>'上级会员','type'=>'member'),
			array('name'=>'mobile','title'=>'手机_mobile','type'=>'string'),
			array('name'=>'wxapptitle','title'=>'对应小程序','type'=>'string'),
			array('name'=>'creditdata','title'=>'积分/余额/虚拟币','type'=>'credit'),
			array('name'=>'nocheck','title'=>'禁用？','type'=>'switch','set'=>'set'),
			array('name'=>'createtime','title'=>'注册日期','type'=>'time'),
			array('name'=>'creditlog','title'=>'积分/余额日志','type'=>'href'),
		),'action'=>array(
			array('type'=>'href','op'=>'mass','title'=>'发送消息'),
			array('type'=>'modal','op'=>'recharge','title'=>'充值'),
			array('type'=>'modal','op'=>'cpassword','title'=>'重置密码'),
			array('type'=>'modal','op'=>'formdata','title'=>'表单信息'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'会员ID','name'=>'id','help'=>''),
			array('type'=>'string','width'=>'15','title'=>'手机号','name'=>'mobile','help'=>''),
			array('type'=>'string','width'=>'15','title'=>'会员账号','name'=>'username','help'=>''),
			array('type'=>'string','width'=>'15','title'=>'昵称','name'=>'nickname','help'=>''),
			array('type'=>'select','width'=>'15','title'=>'会员等级','name'=>'groupid','help'=>'','options'=>'groups'),
		),
	);
	
	$return['htmls']=$htmls;
	//$return['page_type']='list';
	exi($return);
}
if($_RQ['op']=='zhun'){
	//会员列表
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'onemid'=>0,'is_virtual'=>0,'nickname'=>'');
	if($_RQ['id']){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	if(isset($_RQ['groupid'])){
		$params['groupid']=intval($_RQ['groupid']);
		$return['params']['groupid']=$_RQ['groupid'];
	}
	if(!empty($_RQ['nickname'])){
		$params['nickname like']='%'.$_RQ['nickname'].'%';
		$return['params']['nickname']=$_RQ['nickname'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if(!empty($_RQ['username'])){
		$params['username like']='%'.$_RQ['username'].'%';
		$return['params']['username']=$_RQ['username'];
	}
	if(!empty($_RQ['fromuser'])){
		$params['fromuser']=intval($_RQ['fromuser']);
		$return['params']['fromuser']=$_RQ['fromuser'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:10;
	$fields=array('id','avatar','nickname','username','mobile','credit1','credit2','credit3','credit4','credit5','credit6','createtime','fromuser','from_os');
	$list=pdo_getslice('core_members',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['creditlog']=array('action'=>'member.creditlog','title'=>"会员{$item['nickname']}的积分日志",'query'=>'mid='.$item['id']);
			$item['wxgid']=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['id']),'wxgid');
			$item['wxapptitle']=pdo_getcolumn('core_account_wxapp',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['wxgid']),'title');
			if($item['from_os']==1 && $item['wxgid'] && !$item['wxapptitle']){
				$item['wxapptitle']='小程序已删除';
			}
			$item['from_os']=get_ostitle()[$item['from_os']];
			$item['creditdata']=model('core/member')->creditdata($item);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'export'=>'export',
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：
		</br>【无粉丝会员】：是指H5，PC端注册的会员,还没有昵称头像等信息！
		','del_message'=>'确认删除该会员吗？','total'=>$total,'psize'=>$psize,'del_op'=>'del','html_to'=>'list_html','list'=>array(
			array('title'=>'会员ID_member ID','type'=>'img_string',
			'data'=>array(
				array('title'=>'会员ID：','name'=>'id'),
				array('title'=>'推荐人id：','name'=>'fromuser')
				)
			),
			array('title'=>'账号_username','type'=>'img_string','img'=>'avatar',
			'data'=>array(
				array('title'=>'账号：','name'=>'username'),
				array('title'=>'昵称：','name'=>'nickname','unit'=>'from_os')
				)
			),
			array('name'=>'mobile','title'=>'手机_mobile','type'=>'string'),
			array('name'=>'wxapptitle','title'=>'对应小程序','type'=>'string'),
			array('name'=>'creditdata','title'=>'积分/余额/虚拟币','type'=>'credit'),
			array('name'=>'createtime','title'=>'注册日期','type'=>'time'),
			array('name'=>'creditlog','title'=>'积分/余额日志','type'=>'href'),
		),'action'=>array(
			array('type'=>'href','op'=>'mass','title'=>'发送消息'),
			array('type'=>'modal','op'=>'recharge','title'=>'充值'),
			array('type'=>'modal','op'=>'cpassword','title'=>'重置密码'),
			array('type'=>'modal','op'=>'formdata','title'=>'表单信息'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'会员ID','name'=>'id','help'=>''),
			array('type'=>'string','width'=>'15','title'=>'手机号','name'=>'mobile','help'=>''),
			array('type'=>'string','width'=>'15','title'=>'会员账号','name'=>'username','help'=>''),
			array('type'=>'string','width'=>'15','title'=>'昵称','name'=>'nickname','help'=>''),
			array('type'=>'select','width'=>'15','title'=>'会员等级','name'=>'groupid','help'=>'','options'=>'groups'),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='xnlist'){
	//会员列表
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'is_virtual'=>1);
	if($_RQ['id']){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	if(isset($_RQ['groupid'])){
		$params['groupid']=intval($_RQ['groupid']);
		$return['params']['groupid']=$_RQ['groupid'];
	}
	if(!empty($_RQ['nickname'])){
		$params['nickname like']='%'.$_RQ['nickname'].'%';
		$return['params']['nickname']=$_RQ['nickname'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if(!empty($_RQ['username'])){
		$params['username like']='%'.$_RQ['username'].'%';
		$return['params']['username']=$_RQ['username'];
	}
	if(!empty($_RQ['fromuser'])){
		$params['fromuser']=intval($_RQ['fromuser']);
		$return['params']['fromuser']=$_RQ['fromuser'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:10;
	$fields=array('id','avatar','nickname','username','mobile','credit1','credit2','createtime','fromuser','from_os');
	$list=pdo_getslice('core_members',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['creditlog']=array('action'=>'member.creditlog','title'=>"会员{$item['nickname']}的积分日志",'query'=>'mid='.$item['id']);
			$item['wxgid']=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['id']),'wxgid');
			$item['wxapptitle']=pdo_getcolumn('core_account_wxapp',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['wxgid']),'title');
			if($item['from_os']==1 && $item['wxgid'] && !$item['wxapptitle']){
				$item['wxapptitle']='小程序已删除';
			}
			$item['from_os']=get_ostitle()[$item['from_os']];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'真实会员列表'),
			array('op'=>'xnlist','title'=>'虚拟会员列表'),
			array('op'=>'ex_demo','title'=>'导入虚拟用户'),
			array('op'=>'chack_del','title'=>'清空虚拟用户'),
		),
		'export'=>'export',
		'list'=>array('data'=>'list','del_message'=>'确认删除该会员吗？','total'=>$total,'psize'=>$psize,'del_op'=>'del','html_to'=>'list_html','list'=>array(
			array('name'=>'id','title'=>'会员ID_member ID','type'=>'string'),
			array('title'=>'账号_username','type'=>'img_string','img'=>'avatar',
			'data'=>array(
				array('title'=>'账号：','name'=>'username'),
				array('title'=>'昵称：','name'=>'nickname','unit'=>'from_os')
				)
			),
			array('name'=>'wxapptitle','title'=>'对应小程序','type'=>'string'),
			array('name'=>'mobile','title'=>'手机_mobile','type'=>'string'),
			array('name'=>'fromuser','title'=>'推荐人id_fromuser ID','type'=>'string'),
			array('name'=>'credit1','title'=>'积分','type'=>'string'),
			array('name'=>'credit2','title'=>'余额','type'=>'string'),
			array('name'=>'createtime','title'=>'注册日期','type'=>'time'),
			array('name'=>'creditlog','title'=>'积分/余额日志','type'=>'href'),
		),'action'=>array(
			array('type'=>'modal','op'=>'recharge','title'=>'充值'),
			array('type'=>'modal','op'=>'cpassword','title'=>'重置密码'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'会员ID','name'=>'id','help'=>''),
			array('type'=>'string','width'=>'15','title'=>'手机号','name'=>'mobile','help'=>''),
			array('type'=>'string','width'=>'15','title'=>'会员账号','name'=>'username','help'=>''),
			array('type'=>'string','width'=>'15','title'=>'昵称','name'=>'nickname','help'=>''),
			array('type'=>'select','width'=>'15','title'=>'会员等级','name'=>'groupid','help'=>'','options'=>'groups'),
		),
	);
	
	$return['htmls']=$htmls;
	//$return['page_type']='list';
	exi($return);
}
if($_RQ['op']=='heti'){
	//会员合并列表
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'onemid'=>0,'is_virtual'=>0,'mobile <>'=>'');
	if($_RQ['id']){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	if(isset($_RQ['groupid'])){
		$params['groupid']=intval($_RQ['groupid']);
		$return['params']['groupid']=$_RQ['groupid'];
	}
	if(!empty($_RQ['nickname'])){
		$params['nickname like']='%'.$_RQ['nickname'].'%';
		$return['params']['nickname']=$_RQ['nickname'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if(!empty($_RQ['username'])){
		$params['username like']='%'.$_RQ['username'].'%';
		$return['params']['username']=$_RQ['username'];
	}
	if(!empty($_RQ['fromuser'])){
		$params['fromuser']=intval($_RQ['fromuser']);
		$return['params']['fromuser']=$_RQ['fromuser'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:500;
	$fields=array('id','avatar','nickname','username','mobile','credit1','credit2','credit3','credit4','credit5','credit6','createtime','fromuser','from_os');
	$list=pdo_getslice('core_members',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as $k=>&$item){
			$params['mobile']=$item['mobile'];
			$num=pdo_count('core_members',$params);
			if($num<=1){
				unset($list[$k]);
				continue;
			}
			$item['creditlog']=array('action'=>'member.creditlog','title'=>"会员{$item['nickname']}的积分日志",'query'=>'mid='.$item['id']);
			$item['wxgid']=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['id']),'wxgid');
			$item['wxapptitle']=pdo_getcolumn('core_account_wxapp',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['wxgid']),'title');
			if($item['from_os']==1 && $item['wxgid'] && !$item['wxapptitle']){
				$item['wxapptitle']='小程序已删除';
			}
			$item['from_os']=get_ostitle()[$item['from_os']];
			$item['creditdata']=model('core/member')->creditdata($item);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'export'=>'export',
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：<red>需要输入手机号，如果存在相同手机号2个以上的，就支持合并。只显示手机号相同的，可以合并的数据！点击合并操作后，只保留老会员！</red>
		</br>1、【<red>多个昵称,头像一样的会员</red>】？每个公众号，每个小程序（微信/支付宝/头条），都会有自己的唯一粉丝会员，固然就会出现多个雷同会员。
		</br>2、【<red>多个雷同会员如何统一</red>】？1、只需绑定同样的手机号，会员就会统一；2、利用微信开放平台统一粉丝会员（这个方法操作较复杂）；
		</br>3、【<red>手机统一会员的原理</red>】？绑定统一手机号后，同手机的会员账户登录时，会统一自动登录一个较老的会员账户，所有的会员ID信息都是较老这个！
		','del_message'=>'确认删除该会员吗？','del_op'=>'del','html_to'=>'list_html','list'=>array(
			array('title'=>'会员ID_member ID','type'=>'img_string',
			'data'=>array(
				array('title'=>'会员ID：','name'=>'id'),
				array('title'=>'推荐人id：','name'=>'fromuser')
				)
			),
			array('title'=>'账号_username','type'=>'img_string','img'=>'avatar',
			'data'=>array(
				array('title'=>'账号：','name'=>'username'),
				array('title'=>'昵称：','name'=>'nickname','unit'=>'from_os')
				)
			),
			array('name'=>'mobile','title'=>'手机_mobile','type'=>'string'),
			array('name'=>'wxapptitle','title'=>'对应小程序','type'=>'string'),
			array('name'=>'creditdata','title'=>'积分/余额/虚拟币','type'=>'credit'),
			array('name'=>'createtime','title'=>'注册日期','type'=>'time'),
			array('name'=>'creditlog','title'=>'积分/余额日志','type'=>'href'),
		),'action'=>array(
			array('type'=>'post','op'=>'uponemid','title'=>'合并会员'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'会员ID','name'=>'id','help'=>''),
			array('type'=>'string','width'=>'15','title'=>'手机号','name'=>'mobile','help'=>''),
			array('type'=>'string','width'=>'15','title'=>'会员账号','name'=>'username','help'=>''),
			array('type'=>'string','width'=>'15','title'=>'昵称','name'=>'nickname','help'=>''),
			array('type'=>'select','width'=>'15','title'=>'会员等级','name'=>'groupid','help'=>'','options'=>'groups'),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='uponemid'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请选择会员！','error');
	}
	$item=pdo_get('core_members',array('id'=>$id));
	model('core/member')->merge_memberdata($item['mobile'],'',$item['mobile']);
	exi('删除成功！');
}
if($_RQ['op']=='export'){
	//会员列表
	
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['id']){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	if(isset($_RQ['level_id'])){
		$params['level_id']=intval($_RQ['level_id']);
		$return['params']['level_id']=$_RQ['level_id'];
	}
	if(!empty($_RQ['nickname'])){
		$params['nickname like']='%'.$_RQ['nickname'].'%';
		$return['params']['nickname']=$_RQ['nickname'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if(!empty($_RQ['username'])){
		$params['username like']='%'.$_RQ['username'].'%';
		$return['params']['username']=$_RQ['username'];
	}
	if(!empty($_RQ['fromuser'])){
		$params['fromuser']=intval($_RQ['fromuser']);
		$return['params']['fromuser']=$_RQ['fromuser'];
	}
	
	$fields=array('id','avatar','nickname','username','mobile','credit1','credit2','createtime','fromuser');
	$list=pdo_getall('core_members',$params,$fields);
	if($list){
		foreach($list as &$item){
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
		}
	}
	$header=array(
		'id'=>'会员ID',
		'username'=>'会员账号',
		'nickname'=>'昵称',
		'mobile'=>'手机号',
		'credit1'=>'账户积分',
		'credit2'=>'账户余额',
		'createtime'=>'注册时间',
	);
	model('core/tools')->export2excel($header,$list,'会员数据');
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_members',array('id'=>$id));
		$item['birth']=$item['birth']?json_decode($item['birth'],true):array();
		$item['pcd']=$item['birth']?json_decode($item['pcd'],true):array();
		$item['openid']=pdo_getcolumn('core_fans',array('mid'=>$id,'uniacid'=>$_SESSION['uniacid']),'openid');
	}else{
		$item=array();
	}
	$return['item']=$item;
	$return['years']=array();
	for($i=1914;$i<2100;$i++){
		$return['years'][]=$i;
	}
	$return['months']=array(1,2,3,4,5,6,7,8,9,10,11,12);
	$return['days']=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31);
	$htmls=array(
		'type'=>'edit',
		'desc'=>'详情',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'会员列表'),
		),
		'edit'=>array(
			array('type'=>'string','title'=>'昵称','name'=>'nickname','help'=>'','must'=>true),
			array('type'=>'copy','title'=>'OPENID','name'=>'openid'),
			array('type'=>'image','title'=>'头像','name'=>'avatar','help'=>''),
			array('type'=>'select','title'=>'会员等级','name'=>'groupid','help'=>'设置会员等级','options'=>'groups','option_key'=>'id','option_title'=>'title'),
			array('type'=>'day','title'=>'生日','name'=>'birth','help'=>''),
			array('type'=>'pcd','title'=>'户籍','name'=>'pcd','help'=>''),
			array('type'=>'string','title'=>'QQ','name'=>'qq','help'=>''),
			array('type'=>'string','title'=>'详细地址','name'=>'address','help'=>''),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='recharge'){
	//充值
	$credit_types=credit_types();
	if($_RQ['postdata']){
		$postdata=get_postdata();
		if(!$postdata['value']){
			exi('请先输入金额/积分！','error');
		}
		if(!$postdata['type']){
			exi('请选择类型！','error');
		}
		$typetitle=$credit_types[$postdata['type']];
		if($postdata['value'] <=0){
			exi($typetitle.'的值不能小于等于0','error');
		}
		if(is_numeric($postdata['value']) === false){	
			exi($typetitle.'的值必须是整数或小数','error');
		}
		if(!isset($postdata['is_add'])){
			exi('请选择增加/减少！','error');
		}
		$mid=intval($postdata['id']);
		$type=trim($postdata['type']);
		$is_add=intval($postdata['is_add']);
		$value=trim($postdata['value']);
		$remark=trim($postdata['remark']);
		$member=pdo_get('core_members',array('id'=>$mid),array('id','nickname','credit1','credit2','credit3','credit4','credit5','credit6'));
		if($is_add==0 && $value>$member['credit'.$type]){
			exi('请减少的['.$typetitle.']不能大于用户拥有的积分！','error');
		}
		//执行操作
		cfc('core')->member_credit($mid,'credit'.$type,$value,$is_add,'core',$remark,'',$_SESSION['uid']);
		exi('操作['.$typetitle.']成功！','','list');
	}
	$id=$_RQ['id']?intval($_RQ['id']):intval($_RQ['mid']);
	if(!$id){
		exi('请先选择要充值的会员！','error');
	}
	$member=pdo_get('core_members',array('id'=>$id),array('id','nickname','credit1','credit2','credit2','credit3','credit4','credit5','credit6'));
	$return['item']=$member;
	$return['types']=credit_types();
	$return['is_adds']=array(
		'1'=>'增加',
		'0'=>'减少',
	);
	$htmls=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'string','name'=>'nickname','title'=>'会员昵称','read'=>true),
		),
	);
	foreach($credit_types as $k=>$cv){
		$htmls['edit'][]=array('type'=>'string','name'=>'credit'.$k,'title'=>'当前'.$cv,'read'=>true);
	}
	$htmls['edit'][]=array('type'=>'radio','name'=>'type','title'=>'积分类型','options'=>'types');
	$htmls['edit'][]=array('type'=>'radio','name'=>'is_add','title'=>'增加/减少','options'=>'is_adds');
	$htmls['edit'][]=array('type'=>'string','name'=>'value','title'=>'额度');
	$htmls['edit'][]=array('type'=>'text','name'=>'remark','title'=>'备注');
	$return['htmls']=$htmls;
	exi($return);
}
	
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'groupid'=>intval($postdata['groupid']),
		'nickname'=>$postdata['nickname'],
		'avatar'=>$postdata['avatar'],
		'qq'=>$postdata['qq'],
		'address'=>$postdata['address'],
		'birth'=>ijson_encode($postdata['birth']),
		'pcd'=>ijson_encode($postdata['pcd'])
	);
	$mid=intval($postdata['id']);
	if($mid){
		pdo_update('core_members',$data,array('id'=>$mid));
	}
	exi('编辑成功！','','list');
}
if($_RQ['op']=='cpassword'){
	//
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('请先选择会员！','error');
		}
		if(!$post['password']){
			exi('请设置密码！','error');
		}
		$update=array(
			'salt'=>random(8),
		);
		$update['password']=get_password($post['password'],$update['salt']);
		pdo_update('core_members',$update,array('id'=>intval($post['id'])));
		exi('密码重置成功！','','goback');
	}
	if($_RQ['mid']){
		$mid=intval($_RQ['mid']);//兼容其他插件通过传入mid来修改密码
	}else{
		$mid=intval($_RQ['id']);
	}
	if(!$mid){
		exi('请先选择会员！','error');
	}
	$item=pdo_get('core_members',array('id'=>$mid),array('id'));
	$htmls=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'password','name'=>'password','title'=>'新密码'),
		),
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='formdata'){
	if($_RQ['mid']){
		$mid=intval($_RQ['mid']);//兼容其他插件通过传入mid来修改密码
	}else{
		$mid=intval($_RQ['id']);
	}
	if(!$mid){
		exi('请先选择会员！','error');
	}
	$item=pdo_get('core_members',array('id'=>$mid),array('id','diyformdata'));
	$item['diyformdata']=$item['diyformdata']?json_decode($item['diyformdata'],true):array();
	foreach($item['diyformdata'] as &$v){
		if($v['type']==5 && $v['data']){
			$v['data'] = explode(";", $v['data']);
			foreach($v['data'] as $k=>&$i){
				if(empty($v['data'][$k])){
					unset($v['data'][$k]);
				}
			}
		}
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'look',
		'look'=>array(
			array(
						'title'=>'表单详情',
						'type'=>'array',
						'name'=>'diyformdata',
						'list'=>array(
							array('type'=>'string','name'=>'title','title'=>'表单标题'),	
							array('type'=>'labelarray','name'=>'data','title'=>'表单内容'),	
						),
			)
		),
	);
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='mass'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$msgtype=intval($post['msgtype']);
		$fans=pdo_get('core_fans',array('mid'=>intval($post['mid']),'uniacid'=>$_SESSION['uniacid']),array('id','wxgid','openid','tagid_list'));
		if(!$fans){
			exi('指定粉丝不存在！','error');
		}
		if($msgtype){
			if(!$post['news']){
				exi('请选择文章！','error');
			}
			foreach($post['news'] as &$nv){
				$nv['url']='core/article.list/detail/id='.$nv['id'];
			}
			cfc('account')->send_news($post['news'],$fans['openid']);
		}else{
			if(!$post['content']){
				exi('请输入消息内容！','error');
			}
			cfc('account')->send_text($post['content'],$fans['openid']);
		}
		exi('发送消息成功！');
	}
	if($_RQ['mid']){
		$mid=intval($_RQ['mid']);//兼容其他插件通过传入mid来修改密码
	}else{
		$mid=intval($_RQ['id']);
	}
	if(!$mid){
		exi('请先选择会员！','error');
	}
	$item=pdo_get('core_fans',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),array('id','mid','wxgid','nickname'));
	$item['msgtype']='0';
	$return['item']=$item;
	$return['tags']=$tags;
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'tip','title'=>'[<red>温馨提示</red>]：认证服务号，才可以发送消息，且该粉丝必须在48小时内有交互（跟公众号有信息交互，比如触发关键词，发送客服消息等！）'),
			array('type'=>'string','name'=>'nickname','title'=>'粉丝昵称','read'=>true),
			array('type'=>'radio','name'=>'msgtype','title'=>'消息类型','options'=>'msgtype'),
			array('type'=>'choose_many','name'=>'news','title'=>'选择文章','action'=>'core/article.article/get_article/many=1','show'=>array('msgtype'=>'1')),
			array('type'=>'text_wx','tovalue'=>'pagedata.item.content','name'=>'content','title'=>'文本内容'),
		),
	);
	$return['msgtype']=array(0=>'文本',1=>'文章');
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('core_members',array('id'=>$id));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('core_members',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='ex_demo'){
	$nums= pdo_count('core_members',array('uniacid'=>$_SESSION['uniacid'],'is_virtual'=>1),'id');
	if($nums>0){
		exi('已导入虚拟用户，不能重复导入!','error');
	}else{
			$url=ROOT_AT .'../core/resource/username.txt';
			if(!file_exists($url)){
				exi('虚拟用户文件不存在，请联系管理员！','error');
			}
			$txtdata = file_get_contents($url);
			$txtdata = explode("\r\n",$txtdata);
			foreach($txtdata as $k=>&$v){
				if($v){
					$sex=rand(0,1);
					$data=array(
						'uniacid'=>$_SESSION['uniacid'],
						'nickname'=>trim($v),
						'username'=>trim($v),
						'avatar'=>'../core/resource/images/noavatar.gif',
						'is_virtual'=>1,
						'sex'=>$sex,
					);
					pdo_insert('core_members',$data);
				}
			}
			exi('导入成功!','','list');
	}
}
if($_RQ['op']=='chack_del'){
	confirm('确定要清空虚拟数据吗？删除虚拟数据可能导致，已用虚拟用户的地方出错！','ex_del','list');
}
if($_RQ['op']=='ex_del'){
	$nums= pdo_count('core_members',array('uniacid'=>$_SESSION['uniacid'],'is_virtual'=>1),'id');
	if($nums==0){
		exi('已清空虚拟用户！','error','list');
	}
	pdo_delete('core_members',array('uniacid'=>$_SESSION['uniacid'],'is_virtual'=>1));
	exi('清空成功！','','list');
}
if($_RQ['op']=='daoru'){
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['excel']){
			exi('请先上传CSV文件！','error');
		}
		$excel=cfc('files')->local($post['excel']);
		$excel=cfc('tools')->importExcel($excel,true);
		$res=cfc('member')->get_csvitems($excel);
		foreach($res as $item){
			if(!$item['mobile']){
				continue;
			}
			$data=array(
					'credit1'=>$item['credit1'],
					'credit2'=>$item['credit2'],
					'credit3'=>$item['credit3'],
					'credit4'=>$item['credit4'],
					'credit5'=>$item['credit5'],
					'credit6'=>$item['credit6'],
			);
			$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'mobile'=>$item['mobile']));
			if($member['id']){
				if(!$post['type']){
					continue;
				}elseif($post['type']==1){
					pdo_update('core_members',$data,array('id'=>$member['id']));
				}elseif($post['type']==2){
					if($item['credit1']>0){
						model('core/member')->member_credit($member['id'],'credit1',$item['credit1'],true,'core','批量导入');
					}
					if($item['credit2']>0){
						model('core/member')->member_credit($member['id'],'credit2',$item['credit2'],true,'core','批量导入');
					}
					if($item['credit3']>0){
						model('core/member')->member_credit($member['id'],'credit3',$item['credit3'],true,'core','批量导入');
					}
					if($item['credit4']>0){
						model('core/member')->member_credit($member['id'],'credit4',$item['credit4'],true,'core','批量导入');
					}
					if($item['credit5']>0){
						model('core/member')->member_credit($member['id'],'credit5',$item['credit5'],true,'core','批量导入');
					}
					if($item['credit6']>0){
						model('core/member')->member_credit($member['id'],'credit6',$item['credit6'],true,'core','批量导入');
					}
				}
			}else{
				$data['uniacid']=$_SESSION['uniacid'];
				$data['mobile']=$item['mobile'];
				$data['username']=$item['mobile'];
				pdo_insert('core_members',$data);
			}
		}
		cfc('files')->source_del($post['excel']);
		exi('导入成功！','','list');
	}
	$return['item']=array();
	$return['item']['excelurl']=SITEROOT.'/core/resource/images/member.csv';
	$return['htmls']=array (
		  'type' => 'edit',
		  'edit' => array (
				array ('type' => 'tip','title' => '<red>[温馨提示]：在导入过程中，请不要进行任何操作!</br>功能介绍：可将会员快速导入会员列表,节约您的大量时间!</br>使用方法：</br> 1. 将您获取到的CSV文件,并且导入</br>2. 根据手机号，导入会员积分，虚拟币，手机号不存在的用户，将创建新会员，手机号已经存在的，将忽略操作?还是追加积分？</br>3.最后，确认上传即可</red>'),
				array ('name' => 'excelurl','type' => 'copy','title' => 'CSV示例文件下载'),
				array ('name' => 'excel','type' => 'upfile','title' => '上传CSV'),
				array ('name' => 'type','type' => 'radio','title' => '手机号已经存在的操作类型','options'=>'types'),
		  ),
	);
	$return['types']=array('忽略操作','覆盖积分/虚拟币','追加积分/虚拟币');
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先操作用户！','error');
	}
	$post=get_postdata();
	if(isset($post['nocheck'])){
		pdo_update('core_members',array('nocheck'=>intval($post['nocheck'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
?>